#include <vector>
#include <algorithm>
using namespace std;
/*
 * @lc app=leetcode.cn id=47 lang=cpp
 *
 * [47] 全排列 II
 */

// @lc code=start
class Solution {
public:

    vector<vector<int>> res;
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end()); vector<int> temp;
        vector<bool> used(nums.size(), false);
        permulate(temp, nums, used);
        return res;
    }

    void permulate(vector<int>& now, const vector<int>& nums, vector<bool>& used) {
        if (nums.size() == now.size()) {
            res.push_back(now);
            return;
        }
        int last = 11;
        for (int i = 0; i < nums.size(); i++) {
            if (!used[i] && (last == 11 || nums[i] != last)) {
                last = nums[i];
                now.push_back(nums[i]); used[i] = true;
                permulate(now, nums, used);
                now.pop_back(); used[i] = false;
            }
        }
    }
};
// @lc code=end

